package com.kimstore.dao.impl;

import javax.persistence.Query;

import com.kimstore.dao.CustomerDao;
import com.kimstore.bean.Customer;
import com.kimstore.bean.Staff;
import java.util.List;

public class CustomerDaoImpl extends GenericJpaDaoImpl<Customer, Integer> implements CustomerDao {

    @Override
    public Customer findByName(String name) {
        Query query = getEntityManager().createQuery("from Customer customer where customer.name = :name").setParameter("name", name);
        Customer customer = (Customer) getSingleResultOrNull(query);

        return customer;
    }

    public boolean existsName(String name) {
        Query query = getEntityManager().createQuery("select count(customer) from Customer customer where customer.name = :name");
        query.setParameter("name", name);
        long count = (long) query.getSingleResult();

        return count > 0;
    }

    @Override
    public List<Customer> findAll(int startIndex, int maxNumber) {
        Query query = getEntityManager().createQuery("from Customer customer order by customer.name");
        query.setFirstResult(startIndex);
        query.setMaxResults(maxNumber);
        
        List<Customer> customers = query.getResultList();
        
        return customers;
    }

    @Override
    public long count() {
        Query query = getEntityManager().createQuery("select count(customer) from Customer customer");
        
        long count = (long) query.getSingleResult();
        
        return count;
    }
}
